<template>
  <div class="topbar">
    <!-- 版心 -->
    <div class="wrap wrapTopbar">
      <div class="l">欢迎来到叩丁狼积分商城</div>
      <ul class="r">
        <li>
          <img class="avator" :src="userInfo.headImg" alt="" />
          用户名：{{ userInfo.nickName }}
        </li>
        <li>我的积分</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="cart-btn" v-if="isLogined">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>{{ cartTotal }}</strong>
        </li>
        <li class="login-btn" @click="changeIsShowLoginModal(true)" v-else>
          登录
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapActions, mapMutations, mapState } from "vuex";
import { WeChatLoginAPI, UserProfilesAPI } from "../request/api";
export default {
  data() {
    return {};
  },
  computed: {
    ...mapState({
      isLogined: (state) => state.loginStatus.isLogined,
      cartTotal: (state) => state.userInfo.cartTotal,
      userInfo: (state) => state.userInfo.userInfo,
    }),
  },
  methods: {
    ...mapMutations({
      changeIsShowLoginModal: "showModal/changeIsShowLoginModal",
      changeIsLogined: "loginStatus/changeIsLogined",
      initUserInfo: "userInfo/initUserInfo",
    }),
    ...mapActions({
      asyncChangeUserInfo: "userInfo/asyncChangeUserInfo",
       
      asyncChangeIsShowToast:"toastShow/asyncChangeIsShowToast"
    }),
  },
  created() {
    //有可能会获取不到code  使用$nextTick()!!!!!!!
  

    this.$nextTick(async () => {
      //只要用户扫码了 页面就会重新刷新 此时可以获取一下code 的值
      // console.log("this.$route", this.$route.query.code);
      //地址栏中没有code时
      if (!this.$route.query.code) {
        // 路由地址变化后，判断当前用户token是否还存在
        // 如果存在，为登录状态
        // 否则为未登录
        let myToken = localStorage.getItem("x-auth-token");
        this.changeIsLogined(Boolean(myToken));
        if (myToken) {
          //登陆状态
     
          this.asyncChangeUserInfo();
        } else {
          //未登录状态，初始化用户信息
     
          this.initUserInfo();
        }
        return;
      }

      let res = await WeChatLoginAPI({ code: this.$route.query.code });
      // console.log("WeixinLogiinAPI", res);
      // 0 400 407
      if (res.code == 0) {
        //正常情况
        // 1 提示登录成功
        //调用自定义弹窗
        this.asyncChangeIsShowToast({
          toastMsg: "登录成功",
          toastType: "success",
        });

        // //2 存token
        localStorage.setItem("x-auth-token", res["x-auth-token"]);
        // 3.改变登录状态(换成购物车图标)
        this.changeIsLogined(true);
        // 4.清除地址栏中的code
        this.$router.push(this.$route.path);
        //5 .更新用户信息
        this.asyncChangeUserInfo();
      } else if (res.code == 400) {
        //提示用户重新扫码
        //1提示二维码失效
  
          //调用自定义弹窗
        this.asyncChangeIsShowToast({
          toastMsg: "二维码失效",
          toastType: "danger",
        });
        //2 .打开模态窗
        this.changeIsShowLoginModal(true);
      } else if (res.code == 407) {
        //1.提示用户再使用手机号码验证登录一次
     
          //调用自定义弹窗
        this.asyncChangeIsShowToast({
          toastMsg: "再使用手机号码验证登录一次",
          toastType: "warning",
        });
        //2.把登录模块框打开
        this.changeIsShowLoginModal(true);
        //3.存储uuid(登录时取出来)
        localStorage.setItem("uuid", res.uuid);
      }
    });
  },
};
</script>

<style lang="less" scoped>
.topbar {
  // width: 1920px;
  height: 40px;
  background: #242b39;
  font-size: 14px;
  font-family: SourceHanSansSC;
  font-weight: 300;
  color: #fffefe;
  line-height: 40px;
  .wrapTopbar {
    display: flex;
    justify-content: space-between;
    .r {
      li {
        cursor: pointer;
        float: left;
        margin-left: 20px;
        &.login-btn {
          width: 124px;
          height: 40px;
          background: #0a328e;
          text-align: center;
        }
      }
      .cart-btn {
        width: 124px;
        height: 40px;
        background: #0a328e;
        text-align: center;
        img {
          width: 20px;
          height: 19px;
          vertical-align: middle;
        }
        span {
          margin: 0 8px;
        }
        strong {
          width: 22px;
          height: 22px;
          line-height: 22px;
          display: inline-block;
          border-radius: 50%;
          background-color: #fd604d;
        }
      }
      .avator {
        width: 23px;
        vertical-align: middle;
        border-radius: 50%;
      }
    }
  }
}
</style>
